/*
 * This file is part of Space Invaders: Rehash
 * Copyright (C)2007 Vicent Marti (tanoku@gmail.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
 // gfx.h -- header for opengl graphic function wrappers
 // TODO: make this crap portable, took a shitload of work to write so
 // I might as well reuse it

#ifndef	_GFX_H_
#define	_GFX_H_

#include "SDL.h"

#define TEXTURE_COUNT 18
#define MODEL_COUNT 2
#define MENU_TEX_SIZE 512

#define glAlphaBlend() glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
#define glScreenBlend() glBlendFunc( GL_SRC_ALPHA, GL_ONE )

// ------------------------------------------
// --------- Global OpenGL functions --------
// ------------------------------------------
// init the OpenGL system
void glInit();

// set the window size for the first time
void glResizeWindow( int width, int height );

// translate from screen coordinates (top left is 0,0) to 3D world coordinates
void glScreenToWorld( int x, int y, float &world_x, float &world_y, float &world_z );

// switch to ortographic projection (really fast, uses the matrix stack)
void glEnable2D();

// switch back to perspecive projection (fast again)
void glDisable2D();
// ------------------------------------------


// ------------------------------------------
// ------ Primitive drawing functions -------
// ------------------------------------------
// draw a 2D rectangle in orthographic projection
void glDrawRect(int x, int y, int width, int height, float r, float g, float b, float alpha = 1.0f);

// draw the outline of a 2D rectangle, given its width
void glDrawOutlinedRect(int x, int y, int width, int height, int outline_w, float r, float g, float b, float alpha = 1.0f );

// draw a textured 2D rectangle
void glDrawTexuredRect(int x, int y, int width, int height, 
					   GLuint texture, float r = 1.0f, float g = 1.0f, float b = 1.0f, bool inverted = false );

// draw a 3D cube in perspective
void glDrawCube( float x, float y, float z, float size, 
				 float r, float g, float b, float rotation = 0.0f,
				 float rx = 0.0f, float ry = 0.0f, float rz = 0.0f );


// draw the front face of a cube mapped to an optional texture (doesn't support rotation)
void glDrawCubeFace( float x, float y, float z, float size, 
					 float r, float g, float b, GLuint texture = 0);
// ------------------------------------------


// ------------------------------------------
// -------- Text printing functions ---------
// ------------------------------------------
// print text in ortographic projection (line breaks are discarded, use glPrint_br instead)
void glPrint( int font, int x, int y, const char *fmt );

// print text with replacement arguments in ortographic projection
void glPrintf( int font, int x, int y, const char *fmt, ... );

// print text with replacement arguments and automatic line breaks in ortographic projection
void glPrintf_br( int font, int x, int y, const char *fmt, ... );

// print text with automatic line breaks in ortographic projection
void glPrint_br( int font, int x, int y, const char *fmt );
// ------------------------------------------


// ------------------------------------------
// ----------- Texture management -----------
// ------------------------------------------
// load a single BMP file
GLuint glLoadTexture(const char *texture_file);

// load a single BMP file, creating it's alpha channel on the fly based on a colorkey
GLuint glLoadTextureCK(const char *texture_file, int ckr, int ckg, int ckb);

// build the textures list
void glLoadTextures();

// generate an empty texture
GLuint glGetEmptyTex();
// ------------------------------------------


// ------------------------------------------
// --- OBJ Model Files Internal Structure ---
// ------------------------------------------
struct ObjVertex
{
	float x, y, z;
};

typedef ObjVertex ObjNormal;

struct ObjTexCoord
{
	float u, v;
};

struct ObjTriangle
{
	int Vertex[3];
	int Normal[3];
	int TexCoord[3];
};

struct ObjModel
{
	int nVertex, nNormal, nTexCoord, nTriangle;

	ObjVertex* VertexArray;
	ObjNormal* NormalArray;
	ObjTexCoord* TexCoordArray;
	ObjTriangle* TriangleArray;
};
// ------------------------------------------


// ------------------------------------------
// ------ OBJ Model Files Management  -------
// ------------------------------------------
// load a *.obj file into memory
size_t glLoadOBJ(const char*, char**);

// parse the data in a loaded OBJ file into our internal arrays
ObjModel* glParseOBJ(char*, size_t);

// load all the models and compile them during runtime to display lists (reaaaally fast drawing later on).
void glLoadModels();

// draw an OBJ model file to screen (only if it has been compiled as a static display list)
void glDrawModel( GLuint model, float x, float y, float z, GLuint texture,
				  float r = 1.0f, float g = 1.0f, float b = 1.0f, 
				  float scale = 1.0f, float rotation = 0.0f,
				  float rx = 0.0f, float ry = 0.0f, float rz = 0.0f );

// TODO: draw an OBJ model file not as a display list: glDrawModel( ObjModel *model, ...);
// 
// ------------------------------------------


//EOF

#endif

